#include "paraminfo.h"
#include <QSettings>
#include <QDebug>
#include <QTextCodec>
#include <QFile>
#include <unistd.h>

#define DEBUG_PARAMINFO     0
SBASEPARAM m_sBaseParam;

static QTextCodec* m_qCodecUtf8 = QTextCodec::codecForName("UTF-8");

//基本参数
const char* m_pathParamBase={CFG_PATH"baseParamValue.ini"};
//通讯参数
const char* m_pathParamCom={CFG_PATH"com.ini"};
//Mqtt参数
const char* m_pathParamMqtt={CFG_PATH"mqtt.ini"};
//OTA参数
const char* m_pathParamOta={CFG_PATH"ota.ini"};
//Silos参数
const char* m_pathParamSilos={CFG_PATH"silos.ini"};
//Debug参数
const char* m_pathParamDebug={CFG_PATH"debug.ini"};

static SPARAMINFO1 m_sBaseParamInfoList[]={ 
    {m_pathParamBase,"setpwd","设置密码",(void*)&m_sBaseParam.m_setpwd,VARTYPE_STR,"123456"},
    {m_pathParamBase,"unit","单位",(void*)&m_sBaseParam.m_unit,VARTYPE_STR,"kg"},
    {m_pathParamBase,"lowerLimit","下限",(void*)&m_sBaseParam.m_lowerLimit,VARTYPE_DOUBLE,"1"},
    {m_pathParamBase,"upperLimit","上限",(void*)&m_sBaseParam.m_upperLimit,VARTYPE_DOUBLE,"80"},
    {m_pathParamBase,"saveInterval","保存间隔",(void*)&m_sBaseParam.m_saveInterval,VARTYPE_I32,"30"},
    {m_pathParamBase,"upperUpperLimit","上上限",(void*)&m_sBaseParam.m_upperUpperLimit,VARTYPE_DOUBLE,"100"},
    {m_pathParamBase,"lowerLowerLimit","下下限",(void*)&m_sBaseParam.m_lowerLowerLimit,VARTYPE_DOUBLE,"0"},
    {m_pathParamBase,"collectTime","采集时间",(void*)&m_sBaseParam.m_collectTime,VARTYPE_I32,"500"},
    {m_pathParamBase,"axisCount","轴数",(void*)&m_sBaseParam.m_axisCount,VARTYPE_I32,"1"},
    {m_pathParamBase,"backlight","背光",(void*)&m_sBaseParam.m_backlight,VARTYPE_I32,"5"},
    {m_pathParamBase,"screenSaveTime","屏幕保存时间",(void*)&m_sBaseParam.m_screenSaveTime,VARTYPE_I32,"60"},
    {m_pathParamBase,"deviceID","设备ID",(void*)&m_sBaseParam.m_deviceID,VARTYPE_STR,"********"},
    {m_pathParamBase,"truckNo","车号",(void*)&m_sBaseParam.m_truckNo,VARTYPE_STR,"123"},
    {m_pathParamBase,"addrs","从站IDs",(void*)&m_sBaseParam.m_slaveIDs,VARTYPE_I32LIST,"1|"},
    {m_pathParamBase,"factors","系数",(void*)&m_sBaseParam.m_factors,VARTYPE_VARIANTLIST,"1|1|1|1|1|1"},
    {m_pathParamBase,"zeroWeights","零重",(void*)&m_sBaseParam.m_zeroWeights,VARTYPE_DOUBLELIST,"0|0|0|0|0|0"},
    {m_pathParamBase,"zeroWeight1","零重1",(void*)&m_sBaseParam.m_zeroWeight1,VARTYPE_DOUBLE,"0"},
    {m_pathParamBase,"calThreshold","校准阈值",(void*)&m_sBaseParam.m_calThreshold,VARTYPE_DOUBLE,"5"},
    {m_pathParamBase,"encoderChannel","编码器通道",(void*)&m_sBaseParam.m_encoderChannel,VARTYPE_I32,"0"},
    {m_pathParamBase,"encoderFiltering","编码器滤波",(void*)&m_sBaseParam.m_encoderFiltering,VARTYPE_I32,"1"},
    {m_pathParamBase,"encoderDirection","编码器方向",(void*)&m_sBaseParam.m_encoderDirection,VARTYPE_I32,"0"},
    {m_pathParamBase,"encoderInterval","编码器间隔",(void*)&m_sBaseParam.m_encoderInterval,VARTYPE_I32,"200"},
    {m_pathParamBase,"encoderSlaveID","编码器从ID",(void*)&m_sBaseParam.m_encoderSlaveID,VARTYPE_I32,"240"},
    {m_pathParamBase,"sensorSlaveID","传感器从ID",(void*)&m_sBaseParam.m_sensorSlaveID,VARTYPE_I32,"240"},
    {m_pathParamBase,"pulsePerRev","每转脉冲数",(void*)&m_sBaseParam.m_pulsePerRev,VARTYPE_I32,"4000"},
    {m_pathParamBase,"encoderDecimalPlace","编码器小数位",(void*)&m_sBaseParam.m_encoderDecimalPlace,VARTYPE_I32,"1"},
    {m_pathParamBase,"deviceType","设备类型",(void*)&m_sBaseParam.m_deviceType,VARTYPE_STR,"********"},
    {m_pathParamBase,"accumulateZero","累计零重",(void*)&m_sBaseParam.m_accumulateZero,VARTYPE_DOUBLE,"0"},
    {m_pathParamBase,"autoSave","自动保存",(void*)&m_sBaseParam.m_autoSave,VARTYPE_I32,"1"},
    {m_pathParamBase,"saveDays","保存天数",(void*)&m_sBaseParam.m_saveDays,VARTYPE_I32,"3"},
    {m_pathParamBase,"language","语言",(void*)&m_sBaseParam.m_language,VARTYPE_I32,"0"},
    {m_pathParamBase,"ngCount","NG次数",(void*)&m_sBaseParam.m_ngCount,VARTYPE_I32,"0"},
    {m_pathParamBase,"okCount","OK次数",(void*)&m_sBaseParam.m_okCount,VARTYPE_I32,"0"},
    {m_pathParamBase,"pwdSwitch","密码开关",(void*)&m_sBaseParam.m_pwdSwitch,VARTYPE_I32,"1"},
    {m_pathParamBase,"gpsSwitch","GPS开关",(void*)&m_sBaseParam.m_gpsSwitch,VARTYPE_I32,"0"},


    {m_pathParamCom,"dt45COM","dt45串口名称",(void*)&m_sBaseParam.m_comName,VARTYPE_STR,"ttyS2"},
    {m_pathParamCom,"dt45Baudrate","dt45波特率",(void*)&m_sBaseParam.m_baudrate,VARTYPE_I32,"38400"},
    {m_pathParamCom,"dt45DataBit","数据位",(void*)&m_sBaseParam.m_dataBit,VARTYPE_I32,"8"},
    {m_pathParamCom,"dt45StopBit","停止位",(void*)&m_sBaseParam.m_stopBit,VARTYPE_I32,"0"},
    {m_pathParamCom,"dt45Parity","dt45校验位",(void*)&m_sBaseParam.m_parity,VARTYPE_I32,"0"},    
    {m_pathParamCom,"comName4g","4G串口名称",(void*)&m_sBaseParam.m_comName4g,VARTYPE_STR,""},
    {m_pathParamCom,"baudrate4g","4G波特率",(void*)&m_sBaseParam.m_baudrate4g,VARTYPE_I32,"115200"},
    {m_pathParamCom,"dataBit4g","4G数据位",(void*)&m_sBaseParam.m_dataBit4g,VARTYPE_I32,"8"},
    {m_pathParamCom,"stopBit4g","4G停止位",(void*)&m_sBaseParam.m_stopBit4g,VARTYPE_I32,"0"},
    {m_pathParamCom,"parity4g","4G校验位",(void*)&m_sBaseParam.m_parity4g,VARTYPE_I32,"0"},
    {m_pathParamCom,"comNameEncoder","编码器串口名称",(void*)&m_sBaseParam.m_comNameEncoder,VARTYPE_STR,""},
    {m_pathParamCom,"baudrateEncoder","编码器波特率",(void*)&m_sBaseParam.m_baudrateEncoder,VARTYPE_I32,"115200"},
    {m_pathParamCom,"dataBitEncoder","编码器数据位",(void*)&m_sBaseParam.m_dataBitEncoder,VARTYPE_I32,"8"},
    {m_pathParamCom,"stopBitEncoder","编码器停止位",(void*)&m_sBaseParam.m_stopBitEncoder,VARTYPE_I32,"0"},
    {m_pathParamCom,"parityEncoder","编码器校验位",(void*)&m_sBaseParam.m_parityEncoder,VARTYPE_I32,"0"},

    {m_pathParamMqtt,"hostName","MQTT服务器名称",(void*)&m_sBaseParam.m_hostName,VARTYPE_STR,"39.105.156.99"},
    {m_pathParamMqtt,"port","MQTT服务器端口",(void*)&m_sBaseParam.m_port,VARTYPE_I32,"7792"},
    {m_pathParamMqtt,"topic","MQTT主题",(void*)&m_sBaseParam.m_topic,VARTYPE_STR,"sensor/********/weight/up"},
    {m_pathParamMqtt,"subTopic","MQTT订阅主题",(void*)&m_sBaseParam.m_subTopic,VARTYPE_STR,"sensor/********/weight/down"},
    {m_pathParamMqtt,"userName","MQTT用户名",(void*)&m_sBaseParam.m_userName,VARTYPE_STR,"labirinth"},
    {m_pathParamMqtt,"password","MQTT密码",(void*)&m_sBaseParam.m_password,VARTYPE_STR,"A$b3C"},
    {m_pathParamMqtt,"clientID","MQTT客户端ID",(void*)&m_sBaseParam.m_clientID,VARTYPE_STR,"*********"},
    {m_pathParamMqtt,"mqttSwitch","MQTT开关",(void*)&m_sBaseParam.m_mqttSwitch,VARTYPE_I32,"1"},
    {m_pathParamMqtt,"mqttInterval","MQTT间隔",(void*)&m_sBaseParam.m_mqttInterval,VARTYPE_I32,"30"},

    {m_pathParamOta,"apn","APN",(void*)&m_sBaseParam.m_apn,VARTYPE_STR,"CMIOT"},
    {m_pathParamOta,"url","URL",(void*)&m_sBaseParam.m_url,VARTYPE_STR,"http://admin-web-service.labirinth.cn"},

    {m_pathParamOta,"version","版本",(void*)&m_sBaseParam.m_version,VARTYPE_STR,"1.0.0"},
    {m_pathParamOta,"devTime","开发时间",(void*)&m_sBaseParam.m_devTime,VARTYPE_STR,"2025-03-11"},

    {m_pathParamSilos,"totalSilos","总Silos数量",(void*)&m_sBaseParam.m_totalSilos,VARTYPE_I32,"1"},
    {m_pathParamSilos,"silosIDList","SilosID列表",(void*)&m_sBaseParam.m_silosIDList,VARTYPE_I32LIST,"1|"},
    {m_pathParamSilos,"silosCapacityList","Silos容量列表(单位:kg)",(void*)&m_sBaseParam.m_silosCapacityList,VARTYPE_I32LIST,"1000000|"},
    {m_pathParamSilos,"lowLevelThreshold","低位阈值0~100(%)",(void*)&m_sBaseParam.m_lowLevelThreshold,VARTYPE_I32,"20"},
    {m_pathParamSilos,"itemsPerPage","每页显示数量:1或4",(void*)&m_sBaseParam.m_itemsPerPage,VARTYPE_I32,"1"},
    {m_pathParamSilos,"ifshowTotal","是否显示合计:0=否 1=是",(void*)&m_sBaseParam.m_ifshowTotal,VARTYPE_I32,"0"},
    {m_pathParamSilos,"perPageShowtime","每页显示时间(单位:s)",(void*)&m_sBaseParam.m_perPageShowtime,VARTYPE_I32,"10"},
    {m_pathParamSilos,"silosName","Silos名称列表",(void*)&m_sBaseParam.m_silosName,VARTYPE_STRLIST,"料罐1|"},

    {m_pathParamDebug,"debug4g","打印4G调试信息:0=否 1=是",(void*)&m_sBaseParam.m_debug4g,VARTYPE_I32,"0"},
    {m_pathParamDebug,"debugGps","打印GPS调试信息:0=否 1=是",(void*)&m_sBaseParam.m_debugGps,VARTYPE_I32,"0"},
    {m_pathParamDebug,"debugGateway","打印网关调试信息:0=否 1=是",(void*)&m_sBaseParam.m_debugGateway,VARTYPE_I32,"0"},
   
};

QMap<QByteArray,SPARAMINFO1*> m_qBaseParamInfoMap;

//从ini中读取字符串
//返回-1 文件打开失败  0 读取成功  1键不存在
bool GetXtIniString(void* qXtIniSetShare,const char* pKey,QString& pValueStr)
{
    pValueStr = "";
    if(!qXtIniSetShare)
        return false;
    if(!((QSettings*)qXtIniSetShare)->contains(pKey))
    {
        return false;
    }
    pValueStr = ((QSettings*)qXtIniSetShare)->value(pKey).toString();
    return true;
}
//保存字符串到ini
//0成功 -1失败
bool SetXtIniString(void* qXtIniSetShare,const char* pKey,QString pValueStr)
{
    if(!qXtIniSetShare)
    {
        #if DEBUG_PARAMINFO
        qDebug("SetXtIniString key=%s,qXtIniSetShare cannot be null",pKey);
        #endif
        return false;
    }
    if(((QSettings*)qXtIniSetShare)->value(pKey) == pValueStr)
    {
        //相同则不再写入,减少FLASH损耗
        return true;
    }
    ((QSettings*)qXtIniSetShare)->setValue(pKey,pValueStr);
    return true;
}


void* XtIniOpenNew(const char* pIniPath)
{
    QSettings* qXtIniSetShare = new QSettings(pIniPath,QSettings::IniFormat);
    if(!qXtIniSetShare)
    {
        #if DEBUG_PARAMINFO
        qDebug("XtIniOpenNew faild, path=%s",pIniPath);
        #endif
        return 0;
    }
    qXtIniSetShare->setIniCodec(m_qCodecUtf8);
    return qXtIniSetShare;
}

void XtIniClose(void* qXtIniSetShare)
{
    if(!qXtIniSetShare)
        return;
    delete ((QSettings*)qXtIniSetShare);
}

bool LoadBaseParamValueStrFromIni(const char* pIniPath,const char* pKey,QString& pValueStr)
{
    pValueStr = "";
    void* pSIniSet = XtIniOpenNew(pIniPath);
    if(!pSIniSet)
        return false;
    bool ret = GetXtIniString(pSIniSet,pKey,pValueStr);
    XtIniClose(pSIniSet);
    return ret;
}

bool SaveBaseParamValueStrToIni(const char* pIniPath,const char* pKey,QString pValueStr)
{
    //pStrValue[0] = 0;
    void* pSIniSet = XtIniOpenNew(pIniPath);
    if(!pSIniSet)
        return false;

    bool ret = SetXtIniString(pSIniSet,pKey,pValueStr);

    XtIniClose(pSIniSet);

    #if DEBUG_PARAMINFO
    qDebug("SaveBaseParamValueStrToIni key=%s,value=%s,ret=%d",pKey,pValueStr.toStdString().c_str(),ret);
    #endif
    return ret;
}
bool ParamGetValueStrFromVar(const SPARAMINFO1* pSParamInfo1,QString& pValueStr)
{
    pValueStr = QString("");
    switch (pSParamInfo1->varType) {
        case VARTYPE_I32:
            pValueStr = QString::number(*(int*)pSParamInfo1->pVar);
            break;
        case VARTYPE_STR:
            pValueStr = *((QString*)pSParamInfo1->pVar);
            break;
        case VARTYPE_DOUBLE:
            pValueStr = QString::number(*(double*)pSParamInfo1->pVar);
            break;
        case VARTYPE_I32LIST:
        {
            QList<int>* intList = (QList<int>*)pSParamInfo1->pVar;
            for (int i = 0; i < intList->size(); ++i) {
                pValueStr.append(QString::number(intList->at(i)));
                if (i < intList->size() - 1) {
                    pValueStr.append("|");
                }
            }
            break;
        }
        case VARTYPE_DOUBLELIST:
        {
            QList<double>* doubleList = (QList<double>*)pSParamInfo1->pVar;
            for (int i = 0; i < doubleList->size(); ++i) {
                pValueStr.append(QString::number(doubleList->at(i)));
                if (i < doubleList->size() - 1) {
                    pValueStr.append("|");
                }
            }
            break;
        }
        case VARTYPE_VARIANTLIST:
        {
            QVariantList variantList = *((QVariantList*)pSParamInfo1->pVar);
            for(int i=0; i<variantList.size(); i++){
                // 直接使用QVariant的toString()方法转换为字符串
                pValueStr.append(variantList[i].toString());
                if(i < variantList.size() - 1) {
                    pValueStr.append("|");
                }
            }
            break;
        }
        case VARTYPE_STRLIST:
        {
            QList<QString>* strList = (QList<QString>*)pSParamInfo1->pVar;
            for (int i = 0; i < strList->size(); ++i) {
                pValueStr.append(strList->at(i));
                if (i < strList->size() - 1) {
                    pValueStr.append("|");
                }
            }
            break;
        }
        default:
            return false;
    }
    #if DEBUG_PARAMINFO
    qDebug("ParamGetValueStrFromVar key=%s,value=%s",pSParamInfo1->key,pValueStr.toStdString().c_str());
    #endif
    return true;
}

//参数保存处理
bool Param1SaveProcess(const SPARAMINFO1* pSParamInfo1,bool bForceSave)
{
    QString ValueStr;
    QString iniStr;
    bool ret = false;
    if(ParamGetValueStrFromVar(pSParamInfo1,ValueStr) != true)
    {
        #if DEBUG_PARAMINFO
        qDebug("Param1SaveProcess key=%s 未找到参数值串",pSParamInfo1->key);
        #endif
        return false;
    }
    if(LoadBaseParamValueStrFromIni(pSParamInfo1->pIniPath,pSParamInfo1->key,iniStr) != true)
    {
        #if DEBUG_PARAMINFO
        qDebug("Param1VarTmpSave key=%s,value=%s 未找到参数，直接保存",pSParamInfo1->key,ValueStr.toStdString().c_str());
        #endif
        ret = SaveBaseParamValueStrToIni(pSParamInfo1->pIniPath,pSParamInfo1->key,ValueStr);
        return ret;
    }
    if(ValueStr == iniStr)
    {
        if(bForceSave)
        {
            #if DEBUG_PARAMINFO
            qDebug("Param1VarTmpSave key=%s,value=%s 参数值相同,强制保存",pSParamInfo1->key,ValueStr.toStdString().c_str());
            #endif
            return SaveBaseParamValueStrToIni(pSParamInfo1->pIniPath,pSParamInfo1->key,ValueStr);
        }
        else
        {
            #if DEBUG_PARAMINFO
            qDebug("Param1VarTmpSave key=%s,value=%s 参数值相同,不保存",pSParamInfo1->key,ValueStr.toStdString().c_str());
            #endif
        }
        return true;
    }
    else
    {
        #if DEBUG_PARAMINFO
        qDebug("Param1VarTmpSave key=%s,value=%s 参数值不同,保存",pSParamInfo1->key,ValueStr.toStdString().c_str());
        #endif        
        ret = SaveBaseParamValueStrToIni(pSParamInfo1->pIniPath,pSParamInfo1->key,ValueStr);
        return ret;
    }
}

//根据key和值 存储参数变量
bool ParamSetVarFromKey(const char* pKey, bool bForceSave)
{
    const SPARAMINFO1* pSParamInfo1 = m_qBaseParamInfoMap.value(pKey);
    if(!pSParamInfo1)
    {
        #if DEBUG_PARAMINFO
        qDebug("ParamSetVarFromKey error,key=%s,没有该参数",pKey);
        #endif
        return false;
    }
    return Param1SaveProcess(pSParamInfo1,bForceSave);
}

bool ParamSetVarFromStr(const SPARAMINFO1* pSParamInfo1, QString pValueStr)
{
    if(!pSParamInfo1->pVar)
    {
        return false;
    }
    switch (pSParamInfo1->varType) {
        case VARTYPE_I32:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为int",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            *((int*)pSParamInfo1->pVar) = pValueStr.toInt();
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%d 获取参数值为int",pSParamInfo1->key,*((int*)pSParamInfo1->pVar));
            #endif
            break;
        }
        case VARTYPE_STR:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为QString",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            *((QString*)pSParamInfo1->pVar) = pValueStr;
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 获取参数值为QString",pSParamInfo1->key,(*((QString*)pSParamInfo1->pVar)).toStdString().c_str());
            #endif
            break;
        }
        case VARTYPE_DOUBLE:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为double",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            *((double*)pSParamInfo1->pVar) = pValueStr.toDouble();
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%f 获取参数值为double",pSParamInfo1->key,*((double*)pSParamInfo1->pVar));
            #endif
            break;
        }
        case VARTYPE_I32LIST:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为int列表",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            QStringList list = pValueStr.split("|", Qt::SkipEmptyParts);
            for(int i=0; i<list.length(); i++)
            {
                //判断list.at(i)是否为int类型
                if(list.at(i).toInt() == 0 && list.at(i) != "0")
                {
                    #if DEBUG_PARAMINFO
                    qDebug("ParamSetVarFromStr key=%s,value=%s 列表元素%i不是int类型,返回错误",pSParamInfo1->key,pValueStr.toStdString().c_str(),i);
                    #endif
                    return false;
                }
                ((QList<int>*)pSParamInfo1->pVar)->append(list.at(i).toInt());
                #if DEBUG_PARAMINFO
                qDebug("ParamSetVarFromStr key=%s,value=%d 获取参数值为int列表",pSParamInfo1->key,((QList<int>*)pSParamInfo1->pVar)->at(i));
                #endif
            }
            break;
        }
        case VARTYPE_DOUBLELIST:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为double列表",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            QStringList list = pValueStr.split("|", Qt::SkipEmptyParts);
            for(int i=0; i<list.length(); i++)
            {
                //判断list.at(i)是否为double类型
                if(list.at(i).toDouble() == 0.0 && (list.at(i) != "0.0") && (list.at(i) != "0"))
                {
                    #if DEBUG_PARAMINFO
                    qDebug("ParamSetVarFromStr key=%s,value=%s 列表元素%i不是double类型,返回错误",pSParamInfo1->key,pValueStr.toStdString().c_str(),i);
                    #endif
                    return false;
                }
                ((QList<double>*)pSParamInfo1->pVar)->append(list.at(i).toDouble());
                #if DEBUG_PARAMINFO
                qDebug("ParamSetVarFromStr key=%s,value=%f 获取参数值为double列表",pSParamInfo1->key,((QList<double>*)pSParamInfo1->pVar)->at(i));
                #endif
            }
            break;
        }
        case VARTYPE_VARIANTLIST:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为VARIANTLIST列表",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            QStringList list = pValueStr.split("|", Qt::SkipEmptyParts);
            for(int i=0; i<list.length(); i++)
            {
                //判断list.at(i)是否为VARIANT类型
                if(list.at(i).toFloat() == 0.0 && (list.at(i) != "0.0") && (list.at(i) != "0"))
                {
                    #if DEBUG_PARAMINFO
                    qDebug("ParamSetVarFromStr key=%s,value=%s 列表元素%i不是VARIANT类型,返回错误",pSParamInfo1->key,pValueStr.toStdString().c_str(),i);
                    #endif
                    return false;
                }
                ((QVariantList*)pSParamInfo1->pVar)->append(list.at(i).toFloat());
                #if DEBUG_PARAMINFO
                qDebug("ParamSetVarFromStr key=%s,value=%f 获取参数值为VARIANT列表",pSParamInfo1->key,(((QVariantList*)pSParamInfo1->pVar)->at(i).toFloat()));
                #endif
            }
            break;
        }
        case VARTYPE_STRLIST:
        {
            #if DEBUG_PARAMINFO
            qDebug("ParamSetVarFromStr key=%s,value=%s 设置参数值为QString列表",pSParamInfo1->key,pValueStr.toStdString().c_str());
            #endif
            QStringList list = pValueStr.split("|", Qt::SkipEmptyParts);
            for(int i=0; i<list.length(); i++)
            {
                ((QList<QString>*)pSParamInfo1->pVar)->append(list.at(i));
                #if DEBUG_PARAMINFO
                qDebug("ParamSetVarFromStr key=%s,value=%s 获取参数值为QString列表",pSParamInfo1->key,((QList<QString>*)pSParamInfo1->pVar)->at(i).toStdString().c_str());
                #endif
            }
            break;
        }
        default:
            return false;
    }
    return true;
}

bool ParamVarToDefault(const SPARAMINFO1* pSParamInfo1)
{
    if(!pSParamInfo1->pVar)
    {
        #if DEBUG_PARAMINFO
        qDebug("ParamVarToDefault key=%s,value=%s 变量为空，无需设置默认值",pSParamInfo1->key,pSParamInfo1->defaultValue.toStdString().c_str());
        #endif
        return false;
    }
    #if DEBUG_PARAMINFO
    qDebug("ParamVarToDefault key=%s,value=%s 设置默认值",pSParamInfo1->key,pSParamInfo1->defaultValue.toStdString().c_str());
    #endif
    if(ParamSetVarFromStr(pSParamInfo1,pSParamInfo1->defaultValue) != true)
    {
        return false;
    }
    return Param1SaveProcess(pSParamInfo1,false);
}

bool BaseParamInfo1Load(const SPARAMINFO1* pSParamInfo1)
{
    QString ValueStr;
    if(LoadBaseParamValueStrFromIni(pSParamInfo1->pIniPath,pSParamInfo1->key,ValueStr) != true)
    {
        #if DEBUG_PARAMINFO
        qDebug("Param1Load key=%s,value=%s 未找到参数，设置默认值",pSParamInfo1->key,ValueStr.toStdString().c_str());
        #endif
        return ParamVarToDefault(pSParamInfo1);
    }
    if(ParamSetVarFromStr(pSParamInfo1,ValueStr) != true)
    {
        #if DEBUG_PARAMINFO
        qDebug("Param1Load key=%s,value=%s 设置参数失败，设置默认值",pSParamInfo1->key,ValueStr.toStdString().c_str());
        #endif
        return ParamVarToDefault(pSParamInfo1);
    }
    #if DEBUG_PARAMINFO
    qDebug("Param1Load key=%s,value=%s 设置参数成功",pSParamInfo1->key,ValueStr.toStdString().c_str());
    #endif
    return true;
}

static void LoadAllBaseParamFromIni()
{
    const SPARAMINFO1* pSParamInfo1;
    foreach (pSParamInfo1, m_qBaseParamInfoMap) {
        BaseParamInfo1Load(pSParamInfo1);
    }
}

void BaseParamInfoInit()
{
    if (!m_qCodecUtf8)
        m_qCodecUtf8 = QTextCodec::codecForName("UTF-8");
    
    m_qBaseParamInfoMap.clear();
    unsigned int i;
    SPARAMINFO1* pSInfo1 = m_sBaseParamInfoList;
    for(i=0;i<MYARRAYSIZE(m_sBaseParamInfoList);i++,pSInfo1++)
    {
        m_qBaseParamInfoMap.insert(pSInfo1->key,pSInfo1);
    }
}

int CheckIniProcess(const char* filename)
{
    if(QFile::exists(filename))
    {
        #if DEBUG_PARAMINFO
        qDebug("File exists: %s", filename);
        #endif
        return 0;
    }
    else
    {
        // 文件不存在，创建该文件
        QFile file(filename);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            #if DEBUG_PARAMINFO
            qDebug("Create file failed: %s", file.errorString().toLocal8Bit().constData());
            #endif            
            return -1;
        }
        file.close();        
        // 创建成功
        #if DEBUG_PARAMINFO
        qDebug("Create file success: %s\n",filename);
        #endif
        return 0;
    }    
}

void BaseParamInit()
{
    CheckIniProcess(m_pathParamBase);
    CheckIniProcess(m_pathParamCom);
    CheckIniProcess(m_pathParamMqtt);
    CheckIniProcess(m_pathParamOta);
    sync();
    BaseParamInfoInit();
    LoadAllBaseParamFromIni();

}
